package pong;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class PongMessageHandler 
{
	public static final int MSG_SEND_BALL = 221432;
	public static final int MSG_GAME_COUNTDOWN = 743558;
	public static final int MSG_GAME_END = 935274;
	public static final int MSG_GAME_PAUSE = 382645;

	public PongMessageHandler(){};
	
	public static void sendBall(DataOutputStream os, Ball ball)
	{
		try {
			System.out.println("Enviando uma bola...");
			os.writeInt(MSG_SEND_BALL);
			os.writeDouble(ball.track_x[0]);
			os.writeDouble(ball.track_y[0]);
			os.writeInt(ball.speed);
			os.writeDouble(ball.angle);
			os.flush();
			System.out.println("Fiz flush de uma bola...");
		
		} catch (IOException e) {e.printStackTrace(); }
	}
	
	public static Ball getBall(DataInputStream is)
	{
		Ball new_ball = new Ball();
		try {
			System.out.println("getting ball...");
			is.readInt();
			new_ball.track_x[0] = is.readDouble();
			new_ball.track_y[0] = is.readDouble();
			new_ball.speed = is.readInt();
			new_ball.angle = is.readDouble();
							
		} catch (IOException e) { e.printStackTrace();}
		
		return new_ball;		
	}

	public static void sendCountdown(DataOutputStream os, int number)
	{
		try {
			os.writeInt(MSG_GAME_COUNTDOWN);
			os.writeInt(number);
			os.flush();
			System.out.println("Enviando countdown:"+number);
		
		} catch (IOException e) {e.printStackTrace(); }
	}
	
	public static int getCountdown(DataInputStream is)
	{
		int count_value = -1;
		try {
			is.readInt();
			count_value = is.readInt();
							
		} catch (IOException e) { e.printStackTrace();}
		
		return count_value;		
	}

	
}
